package io.renren.modules.sys.service.impl;

import io.renren.modules.sys.dao.SysUserRoleDao;
import io.renren.modules.sys.entity.*;
import io.renren.modules.sys.service.SysRoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.LinePasswordDao;
import io.renren.modules.sys.service.LinePasswordService;


@Service("linePasswordService")
public class LinePasswordServiceImpl extends ServiceImpl<LinePasswordDao, LinePasswordEntity> implements LinePasswordService {

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数
        String devicesn = null;
        if( params.get("devicesn")!=null&&!params.get("devicesn").equals("")){//设备编号
            devicesn =params.get("devicesn").toString();
        }
        String qcodeid = null;
        if( params.get("qcodeid")!=null&&!params.get("qcodeid").equals("")){//二维码编号id
            qcodeid =params.get("qcodeid").toString();
        }

        Long roleId = sysUserRoleDao.queryRoleId(user.getUserId());
        String agentid = null;
        String salesid = null;
        String merchant = null;
        if(roleId!=null) {
            SysRoleEntity role = sysRoleService.getById(roleId);

            if (role.getRoleName().indexOf("代理商") != -1) {//当前登陆用户为代理商
                agentid = user.getUserId().toString();
            }
            if (role.getRoleName().indexOf("业务员") != -1) {//当前登陆用户为业务员
                salesid = user.getUserId().toString();

            }
            if (role.getRoleName().indexOf("商户") != -1||role.getRoleName().indexOf("商铺") != -1) {//当前登陆用户为店铺
                merchant = user.getUserId().toString();

            }
        }

        int count = baseMapper.queryListcount(devicesn,qcodeid,salesid,merchant,agentid);
        List<LinePasswordEntityVO> list = baseMapper.getQcode(devicesn,qcodeid,salesid,merchant,agentid,(currPage-1)*pageSize,currPage*pageSize);
        return new PageUtils(list,count,pageSize,currPage);
    }


}
